################################################################################
# This script will resample ASCII files from 80 * 80 m to 250 * 250 m
# This script will also merge 3 FPC ASCII files into a mean and variance of FPC file

################################################################################
#load necessary libraries

#list the libraries needed
necessary=c("adehabitat","SDMTools","rgdal","sp","raster")

#check if library is installed
installed = necessary %in% installed.packages()

#if library is not installed, install it
if (length(necessary[!installed]) >=1) install.packages(necessary[!installed], dep = T)

#load the libraries
for (lib in necessary) library(lib,character.only=T)

# Establish in/out directories

dir.80 = '/home1/99/jc152199/MicroclimateStatisticalDownscale/80mASCII/'
dir.250 = '/home1/99/jc152199/MicroclimateStatisticalDownscale/250mASCII/'
solar.dir =  '/home1/99/jc152199/MicroclimateStatisticalDownscale/80mASCII/SOLAR/'

# Read in template ASCII

base.asc = read.asc(paste(dir.250,'fpc2005wtplusbuffer250m.asc',sep=''))

# Create list of unique row/column positions

base.pos = as.data.frame(which(is.finite(base.asc), arr.ind = T))

# Convert row/column to east north

base.pos$east = getXYcoords(base.asc)$x[base.pos$row]
base.pos$north = getXYcoords(base.asc)$y[base.pos$col]

# Create a two column data frame of eastings/northings

pnts = cbind(base.pos[,3],base.pos[,4])

# Create a list of files to re-size

resize = dir(dir.80,pattern='asc')
solar.resize = dir(solar.dir, pattern ='asc')

# Create a loop

for (i in solar.resize) {

t.asc = read.asc(paste(solar.dir,i,sep=''))

cat(head(t.asc),'...\n')

t.data = cbind(base.pos, extract.data(pnts,t.asc))

base.asc[cbind(base.pos$row,base.pos$col)]=t.data[,5]

write.asc(x=base.asc, file=paste(dir.250,i,sep=""))

rm(t.data)

}

# Create the mean and variance ASCII for FPC

fpc.files = dir(dir.250)[3:5]

fpc2005 = read.asc(paste(dir.250,fpc.files[1],sep=""))
fpc2006 = read.asc(paste(dir.250,fpc.files[2],sep=""))
fpc2007 = read.asc(paste(dir.250,fpc.files[3],sep=""))

fpc.pos = as.data.frame(which(is.finite(fpc2005),arr.ind=T))

fpc.pos$east = getXYcoords(fpc2005)$x[fpc.pos$row]
fpc.pos$north = getXYcoords(fpc2005)$y[fpc.pos$col]


fpc.data1 = cbind(fpc.pos, extract.data(cbind(fpc.pos[,3],fpc.pos[,4]),fpc2005))
names(fpc.data1)[5]='fpc'
fpc.data2 = cbind(fpc.pos, extract.data(cbind(fpc.pos[,3],fpc.pos[,4]),fpc2006))
names(fpc.data2)[5]='fpc'
fpc.data3 = cbind(fpc.pos, extract.data(cbind(fpc.pos[,3],fpc.pos[,4]),fpc2007))
names(fpc.data3)[5]='fpc'
fpc.data4 = rbind(fpc.data1, fpc.data2, fpc.data3)

t.mean = function(x){return(mean(x,na.rm=T))}
t.var = function(x){return(var(x, na.rm=T))}


fpcmean = aggregate(fpc.data4[,5], by=list(row=fpc.data4$row, col=fpc.data4$col), FUN = t.mean)
fpcvar = aggregate(fpc.data4[,5], by=list(row=fpc.data4$row, col=fpc.data4$col), FUN= t.var)

names(fpcmean)[3]='fpcmean'
names(fpcvar)[3]='fpcvar'

fpc2005[cbind(fpcmean$row,fpcmean$col)]= fpcmean[,3]
write.asc(x=fpc2005, file=paste(dir.250,'fpcmean.asc',sep=""))

fpc2005[cbind(fpcmean$row,fpcmean$col)]= fpcvar[,3]
write.asc(x=fpc2005, file=paste(dir.250,'fpcvar.asc',sep=""))





